package com.example.bankend.service.method;


import com.example.bankend.mapper.UserMapper;
import com.example.bankend.model.User;
import com.example.bankend.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User registerUser(User user) {
        // 检查用户名是否已存在
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            throw new RuntimeException("该用户名已存在");
        }
        // 插入新用户
        userMapper.insertUser(user);
        return user;
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            return null; // 用户不存在
        }
        if (user.getPassword().equals(password)) {
            return user; // 登录成功
        } else {
            return null; // 密码错误
        }
    }

    @Override
    public void deleteuser(String username){
        userMapper.deleteuser(username);
    }
}
